package com.example.jn_dxs.Ui.fragment;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.jn_dxs.Base.BaseFragment;
import com.example.jn_dxs.Base.BaseViewModel;
import com.example.jn_dxs.Bean.PopuBean;
import com.example.jn_dxs.Bean.SearchBean;
import com.example.jn_dxs.R;
import com.example.jn_dxs.Ui.activity.SingleWellWaterLevelActivity;
import com.example.jn_dxs.Ui.activity.StatisticsActivity;
import com.example.jn_dxs.Ui.adapter.PopuAdapter;
import com.example.jn_dxs.Ui.adapter.SearchAdapter;
import com.example.jn_dxs.Ui.adapter.StatisticsAdapter;
import com.example.jn_dxs.Utlis.LoadingUtil;
import com.example.jn_dxs.ViewModel.SearchVM;
import com.example.jn_dxs.databinding.FragmentMapBinding;
import com.example.jn_dxs.databinding.FragmentStatisticsBinding;

import java.util.ArrayList;
import java.util.List;

public class StatisticsFragment extends BaseFragment<SearchVM, FragmentStatisticsBinding> {
    private SearchAdapter adapter;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_statistics;
    }

    @Override
    protected void loadData() {
        binding.MyTopBar.tvBack.setVisibility(View.GONE);
        binding.MyTopBar.tvTitle.setText("统计");
        adapter =new SearchAdapter(R.layout.item_fragment_statistics);
        binding.RecyclerStatistics.setAdapter(adapter);
        adapter.setListenter((view, popuBean, pos) -> {
            //数据展示
            startActivity(new Intent(getContext(), StatisticsActivity.class).putExtra("id", popuBean.getPkiaa()));
        });
        binding.searchET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                model.getSearch(s.toString());
            }
        });
        LoadingUtil.setStatus(activity, 1);
        model.getSearch(binding.searchET.getText().toString());
        binding.SearchTV.setOnClickListener(v -> {
            model.getSearch(binding.searchET.getText().toString());
        });
    }
    @Override
    protected void onObjectDataChange(JSONObject object) {
        super.onObjectDataChange(object);
        LoadingUtil.hide();
        JSONArray rows = object.getJSONArray("rows");
        int total = object.getIntValue("total");
        List<SearchBean> list = new ArrayList<>();
        if (total > 0) {
            for (int i = 0; i < rows.size(); i++) {
                SearchBean searchBean = JSON.parseObject(rows.getJSONObject(0).toJSONString(), SearchBean.class);
                list.add(searchBean);
            }
            adapter.setNewData(list);
        }
    }
}
